package cc.sunni.article.service.impl;

import cc.sunni.api.utils.PageUtils;
import cc.sunni.api.utils.Query;
import cc.sunni.article.mapper.CommentsDao;
import cc.sunni.article.service.ArticlePortalService;
import cc.sunni.article.service.CommentPortalService;
import cc.sunni.common.Constant;
import cc.sunni.common.utils.RedisOperator;
import cc.sunni.pojo.Comments;
import cc.sunni.pojo.vo.ArticleDetailVO;
import cc.sunni.pojo.vo.CommentsVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("commentsService")
public class CommentPortalServiceImpl extends ServiceImpl<CommentsDao, Comments> implements CommentPortalService {
    @Autowired
    private ArticlePortalService articlePortalService;
    @Autowired
    private RedisOperator redis;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<Comments> page = this.page(
                new Query<Comments>().getPage(params),
                // 拼接查询条件
                new LambdaQueryWrapper<Comments>()
        );

        return new PageUtils(page);
    }

    @Override
    @Transactional
    public void createComment(String articleId, String fatherCommentId, String content, String userId, String nickname, String face) {
        ArticleDetailVO article = articlePortalService.queryDetail(articleId);
        Comments comments = new Comments();
        comments.setWriterId(article.getPublishUserId());
        comments.setArticleTitle(article.getTitle());
        comments.setArticleCover(article.getCover());
        comments.setArticleId(articleId);
        comments.setFatherId(fatherCommentId);
        comments.setCommentUserId(userId);
        comments.setCommentUserNickname(nickname);
        comments.setCommentUserFace(face);
        comments.setContent(content);
        comments.setCreateTime(new Date());
        baseMapper.insert(comments);

        // 评论数累加
        redis.increment(Constant.REDIS_ARTICLE_COMMENT_COUNTS + ":" + articleId, 1);
    }

    @Override
    public PageUtils queryArticleComments(String articleId, Integer page, Integer pageSize) {
        Map<String, Object> map = new HashMap<>();
        map.put("articleId", articleId);
        Page<CommentsVO> pages = new Page<>(page, pageSize);
        //将查询结果封装到page中,作为page中的数据
        pages.setRecords(baseMapper.queryArticleCommentList(pages, map));
        return new PageUtils(pages);
    }

    @Override
    public PageUtils queryWriterCommentsMng(String writerId, Integer page, Integer pageSize) {
        IPage<Comments> pages = this.page(
                new Page<>(page, pageSize),
                // 拼接查询条件
                new LambdaQueryWrapper<Comments>().eq(Comments::getWriterId, writerId)
        );
        return new PageUtils(pages);
    }

    @Override
    public void deleteComment(String writerId, String commentId) {
        baseMapper.deleteById(commentId);
    }
}
